#ifndef BINARYTREE_H
#define BINARYTREE_H

#include <iostream>

using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *create(int arr[], int &index, int n)
{
	if (index < n)
	{
		if (arr[index] == 0)
		{
			index++;
			return NULL;
		}
		TreeNode *root = new TreeNode(arr[index]);
		index++;
		root->left = create(arr, index, n);
		root->right = create(arr, index, n);
		return root;
	}
	else
	{
		return NULL;
	}
}

TreeNode *createBinaryTree(int arr[], int n)
{
	int index = 0;
	return create(arr, index, n);
}

void preorderPrint(TreeNode *root)
{
	if (root)
	{
		cout << root->val << " ";
		preorderPrint(root->left);
		preorderPrint(root->right);
	}
}

void inorderPrint(TreeNode *root)
{
	if (root)
	{
		inorderPrint(root->left);
		cout << root->val << " ";
		inorderPrint(root->right);
	}
}

void postorderPrint(TreeNode *root)
{
	if (root)
	{
		postorderPrint(root->left);
		postorderPrint(root->right);
		cout << root->val << " ";
	}
}

void deleteBinaryTree(TreeNode *root)
{
	if (root)
	{
		deleteBinaryTree(root->left);
		deleteBinaryTree(root->right);
		delete root;
	}
}

#endif
